package com.k.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.k.demo.entity.ConstantTerm;
import com.k.demo.service.ConstantTermService;

@RestController
@RequestMapping("/constant")
public class ConstantTermController {
	
	@Autowired
	ConstantTermService constantTermService;
	
	
	/**
	 * 返回所有常数项
	 * url /constant/all
	 * @author shy
	 */
	@RequestMapping("/all")
	public Object all() {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("ctkList", constantTermService.getAll());
		
		return map;
	}
	
	/**
	 * 返回指定的常数
	 * url /constant/searchById
	 * @author shy
	 */
	@RequestMapping("/searchById")
	public Object getById(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("searchResult", constantTermService.getById((int)map.get("constant_term_id")));
		
		return rmap;
	}
	
	/**
	 * 新增常数项
	 * url /constant/add
	 * @author shy
	 */
	@RequestMapping("/add")
	public Object addCT(@RequestBody ConstantTerm constantTerm) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
//		System.out.println(constantTerm.toString());
		
		rmap.put("addResult", constantTermService.addConstantTerm(constantTerm));
		
		return rmap;
	}
	
	/**
	 * 删除制定的常数项
	 * url /constant/deletebyid
	 * @author shy
	 */
	@RequestMapping("/deletebyid")
	public Object deleteById(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("deleteResult", constantTermService.falseDeleteById((int)map.get("constant_term_id")));
		
		return rmap;
	}
	
	/**
	 * 修改常数项
	 * url /constant/modify
	 * @author shy
	 */
	@RequestMapping("/modify")
	public Object modifyCT(@RequestBody ConstantTerm constantTerm) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("modifyhResult", constantTermService.modifyConstantTerm(constantTerm));
		
		return rmap;
	}
	
	/**
	 * 批量删除
	 * url /constant/deletegroup
	 * @author shy
	 */
	@RequestMapping("/deletegroup")
	public Object deleteCTs(@RequestBody String deleteGroup) {
		
		JSONObject jsonObject = JSON.parseObject(deleteGroup);
		
		List<Integer> deleteList = JSON.parseArray(jsonObject.getString("deleteList"), Integer.class);
		
		System.out.println(deleteList.isEmpty());
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("deleteGroupResult", constantTermService.deletByGroup(deleteList));
		
		return map;
	}
	
	
	/**
	 * 关于常数项的getall并分页显示
	 * url/getallpage
	 * @author shy
	 */
	@RequestMapping("/getallpage")
	public Object getAllByPagesearchNoticeByName(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("searchByNameList", constantTermService.getPage((int)map.get("pageNum"), (int)map.get("pageSize")));
		
		return rmap;
		
	}

	/**
	 * 关于常数项的模糊查询并分页显示
	 * url/searchByContent
	 * @author shy
	 */
	@RequestMapping("/searchByConstantWithPage")
	public Object searchCTByConstantTermWithPage(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();	
		
		ConstantTerm constantTerm = new ConstantTerm();
		
		if("".equals(map.get("constant_term_id")) || map.get("constant_term_id")==null ) {
			constantTerm.setConstant_term_id(null);
		}else {
			constantTerm.setConstant_term_id(Integer.parseInt((String)map.get("constant_term_id")));
		}
		
		if("".equals(map.get("constant_term_kind")) || map.get("constant_term_kind")==null ) {
			constantTerm.setConstant_term_kind(null);
		}else {
			constantTerm.setConstant_term_kind(((int)map.get("constant_term_kind")));
		}
		
		constantTerm.setConstant_term_name((String)map.get("constant_term_name"));
		constantTerm.setConstant_term_meaning((String)map.get("constant_term_meaning"));
		
		System.out.println(constantTerm.toString());
		
		rmap.put("searchByConstantTermList", constantTermService.getPageByConstantTerm((int)map.get("pageNum"),(int)map.get("pageSize"),constantTerm));
		
		return rmap;
		
	}
	
	/**
	 * 关于常数项的模糊查询不分页显示
	 * url/searchByContent
	 * @author shy
	 */
	@RequestMapping("/searchByConstant")
	public Object searchCTByConstantTerm(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();	
		
		ConstantTerm constantTerm = new ConstantTerm();
		
		if("".equals(map.get("constant_term_id")) || map.get("constant_term_id")==null ) {
			constantTerm.setConstant_term_id(null);
		}else {
			constantTerm.setConstant_term_id(Integer.parseInt((String)map.get("constant_term_id")));
		}
		
		if("".equals(map.get("constant_term_kind")) || map.get("constant_term_kind")==null ) {
			constantTerm.setConstant_term_kind(null);
		}else {
			constantTerm.setConstant_term_kind(((int)map.get("constant_term_kind")));
		}
		
		constantTerm.setConstant_term_name((String)map.get("constant_term_name"));
		constantTerm.setConstant_term_meaning((String)map.get("constant_term_meaning"));
		
		System.out.println(constantTerm.toString());
		
		rmap.put("searchByConstantTermList", constantTermService.getByConstantTerm(constantTerm));
		
		return rmap;
		
	}
	
	//根据常数项类别查找
	@RequestMapping("/getByKind")
	public Object getByKind(@RequestParam Integer kind) {
		
		Map<String,Object> map = new HashMap<String, Object>();	
		
		map.put("lvList", constantTermService.getByConstantTermKind(kind));
		
		return map;
	}
}
